using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace ShootThemUp.Sources.GameElements.Ships.Player
{
    public class LifeBar : Sprite
    {
        #region Constant Fields

        #region Life Texutre

        /// <summary>
        /// The width of the life bar texture.
        /// </summary>
        private const int SPRITESHEET_LIFE_TEXTURE_WIDTH = 216;
        /// <summary>
        /// The height of the life bar texture.
        /// </summary>
        private const int SPRITESHEET_LIFE_TEXTURE_HEIGHT = 12;

        /// <summary>
        /// The x position of the life bar texture in the spritesheet.
        /// </summary>
        private const int SPRITESHEET_LIFE_POSITION_X = 2;
        /// <summary>
        /// The y position of the life bar texture in the spritesheet.
        /// </summary>
        private const int SPRITESHEET_LIFE_POSITION_Y = 126;

        #endregion

        #region Out Line Texture

        /// <summary>
        /// The width of the out line texture.
        /// </summary>
        private const int SPRITESHEET_OUTLINE_TEXTURE_WIDTH = 220;
        /// <summary>
        /// The height of the out line texture.
        /// </summary>
        private const int SPRITESHEET_OUTLINE_TEXTURE_HEIGHT = 16;
        /// <summary>
        /// The x position of the outline texture in the spritesheet.
        /// </summary>
        private const int SPRITESHEET_OUTLINE_POSITION_X = 0;
        /// <summary>
        /// The y position of the outline texture in the spritesheet.
        /// </summary>
        private const int SPRITESHEET_OUTLINE_POSITION_Y = 108;

        #endregion

        #region Margins

        /// <summary>
        /// The x margin of the life bar, ie the gap between the left border of the screen and the life bar.
        /// </summary>
        private const int LIFEBAR_MARGIN_X = 12;
        /// <summary>
        /// The y margin of the life bar, ie the gap between the top border of the screen and the life bar.
        /// </summary>
        private const int LIFEBAR_MARGIN_Y = 12;
        /// <summary>
        /// The x margin of the outline, ie the gap between the left border of the screen and the outline.
        /// </summary>
        private const int OUTLINE_MARGIN_X = 10;
        /// <summary>
        /// The y margin of the outline, ie the gap between the top border of the screen and the outline.
        /// </summary>
        private const int OUTLINE_MARGIN_Y = 10;

        #endregion

        #endregion

        #region Fields

        /// <summary>
        /// The texture of the outline of the life bar.
        /// </summary>
        private Sprite outlineLife;

        #endregion

        #region Properties

        public Sprite OutlineLife {
            get { return this.outlineLife;}
            set { this.outlineLife = value; }
        }

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="LifeBar"/> class.
        /// </summary>
        public LifeBar()
        {
            this.Position = new Vector2(LifeBar.LIFEBAR_MARGIN_X, LifeBar.LIFEBAR_MARGIN_Y);
            this.SourceRectangle = new Rectangle(LifeBar.SPRITESHEET_LIFE_POSITION_X, LifeBar.SPRITESHEET_LIFE_POSITION_Y, LifeBar.SPRITESHEET_LIFE_TEXTURE_WIDTH, LifeBar.SPRITESHEET_LIFE_TEXTURE_HEIGHT);
            this.Color = Color.ForestGreen;
            this.outlineLife = new Sprite(new Vector2(LifeBar.OUTLINE_MARGIN_X, LifeBar.OUTLINE_MARGIN_Y),
                                          new Rectangle(LifeBar.SPRITESHEET_OUTLINE_POSITION_X, LifeBar.SPRITESHEET_OUTLINE_POSITION_Y, LifeBar.SPRITESHEET_OUTLINE_TEXTURE_WIDTH, LifeBar.SPRITESHEET_OUTLINE_TEXTURE_HEIGHT));
        }

        #endregion

        #region Load Content

        /// <summary>
        /// Loads the Texture2D in the game.
        /// </summary>
        /// <param name="content">The content manager.</param>
        /// <param name="assetName">Name of the asset to load.</param>
        public override void LoadContent(ContentManager content, String assetName) {
            base.LoadContent(content, assetName);
            this.outlineLife.LoadContent(content, assetName);            
        }

        #endregion

        #region Methods

        /// <summary>
        /// This function draw the life bar in the sprite batch.
        /// </summary>
        /// <param name="spritebatch"> The sprite batch.</param>
        /// <param name="lifePercentage"> The percentage of life left to draw (between 0 and 100).</param>
        public void Draw(SpriteBatch spritebatch, int lifePercentage) {

            //If the percentage of life left is over 100%, we set it to 100.
            if (lifePercentage > 100)
                lifePercentage = 100;

            //If the percentage of life left is under 0%, we set it to 0.
            if (lifePercentage < 0)
                lifePercentage = 0;

            //Draws the out line.
            this.outlineLife.Draw(spritebatch);

            //Sets the color in function of the life left.
            this.SetLifeColor(lifePercentage);

            int barWidth = (LifeBar.SPRITESHEET_LIFE_TEXTURE_WIDTH * lifePercentage) / 100; // The width of the life bar to select in the spritesheet. In function of the life left.
            this.SourceRectangle = new Rectangle(LifeBar.SPRITESHEET_LIFE_POSITION_X, LifeBar.SPRITESHEET_LIFE_POSITION_Y, barWidth, LifeBar.SPRITESHEET_LIFE_TEXTURE_HEIGHT);
           
            base.Draw(spritebatch);            
        }

        /// <summary>
        /// This function sets the color of the life bar in function of the life left.
        /// </summary>
        /// <param name="lifePercentage">The life left, in percent.</param>
        public void SetLifeColor(int lifePercentage)
        {
            if (lifePercentage >= 75)
                this.Color = Color.ForestGreen;
            else if (lifePercentage >= 50)
                this.Color = Color.YellowGreen;
            else if (lifePercentage >= 25)
                this.Color = Color.Orange;
            else
                this.Color = Color.Red;                
        }

        #endregion
    }
}
